package org.project.product.android.pfe_fanta;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.domain.Product;
import org.project.product.android.pfe.fanta.util.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class ProductViewActivity extends ActivityParent {

	private Product product;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.setContentView(R.layout.view_product);
		product = (Product) getIntent().getExtras().getSerializable(
		        Utils.PRODUCT_SELECTED);

		initViewWithProduct();
	}

	private void initViewWithProduct() {
		TextView name = (TextView) findViewById(R.id.label_product_name);
		name.setText(product.getNom());
		TextView poids = (TextView) findViewById(R.id.label_product_poids);
		poids.setText(String.valueOf(product.getPoids()));
		TextView codeBarre = (TextView) findViewById(R.id.label_product_codebarre);
		codeBarre.setText(product.getCodBarre());
		TextView description = (TextView) findViewById(R.id.label_product_description);
		description.setText(product.getDescription());

		TextView quantity = (TextView) findViewById(R.id.label_product_quantitty);
		quantity.setText(String.valueOf(product.getQuantity()));

		TextView prix = (TextView) findViewById(R.id.label_product_prix);
		prix.setText(String.valueOf(product.getPrix()));
	}

	public void editProduct(View v) {
		lancerActivityForResult(this, EditOrDeleteProduct.class, product);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == EditOrDeleteProduct.EDITED && data != null) {
			product = (Product) data.getExtras().getSerializable(
			        Utils.PRODUCT_SELECTED);
			initViewWithProduct();
		}
		if (resultCode == EditOrDeleteProduct.DELETED) {
			finish();
		}
	}

	public void lancerActivityForResult(Context context,
	        Class<? extends Activity> activityClass, Product product) {
		Intent i = new Intent(context, activityClass);
		Bundle bundle = new Bundle();
		bundle.putSerializable(Utils.PRODUCT_SELECTED, product);
		i.putExtras(bundle);
		startActivityForResult(i, 2000);

	}
}
